package com.monlyu.bo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.util.DateUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.vo.DeviceSupply;
import com.monlyu.vo.Devices;

/**
 * @author SongJun
 * @since 2010-2-22
 * @description 设备信息管理
 */
@Repository(value = "devicesBo")
public class DevicesBo extends BaseBo {

	/**
	 * 设备新增
	 * 
	 * @since 2010-2-22
	 */
	private DeviceSupplyBo deviceSupplyBo;

	public void setDeviceSupplyBo(DeviceSupplyBo deviceSupplyBo) {
		this.deviceSupplyBo = deviceSupplyBo;
	}

	public void insertOrUpdate(Devices devices, Long supid) {
		//DeviceSupply dbds = deviceSupplyBo.getDeviceSupplyById(supid);
		//devices.setDevicesupply(dbds);
		if (null != devices.getId()) {
			updateDevices(devices);
		} else {
			// 插入设备编号自定义
			// devices.setDeviceNo(getDeviceNo());
			dao.insertOrUpdate(devices);
		}
	}

	public Devices getDevciceByNo(String deviceNo) {
		String hql = "from com.monlyu.vo.Devices d where d.deviceNo =?";
		return (Devices) dao.findOne(hql, deviceNo);
	}

	// 判断是否已经存在的同类型的设备编号
	public boolean isInclude(Devices de) {
		String hql = "from com.monlyu.vo.Devices d where d.deviceNo =? and d.devicetype=?";
		return (Devices) dao.findOne(hql, de.getDeviceNo(), de.getDevicetype()) == null ? false
				: true;

	}

	public void updateDevices(Devices devices) {
		Devices dbDevices = getDevicesById(devices.getId());
		dbDevices.setDeviceInfo(devices.getDeviceInfo());
		dbDevices.setDeviceName(devices.getDeviceName());
		dbDevices.setDeviceState(devices.getDeviceState());
		dbDevices.setDevicetype(devices.getDevicetype());
		dbDevices.setBuyDate(devices.getBuyDate());
		dbDevices.setDeviceNo(devices.getDeviceNo());
		dbDevices.setEnable(devices.isEnable());
		dbDevices.setDeviceSupply(devices.getDeviceSupply());
		dao.update(dbDevices);
	}

	public Pages<Devices> getDevicesList(Page page) {
		Criteria criteria = dao.initCriteria(Devices.class);
		if (!StringUtil.isEmpty(page.getKey())) {
			criteria.add(Restrictions.like("deviceName", page.getKey().trim(),
					MatchMode.ANYWHERE));
		}
		criteria.addOrder(Order.asc("deviceName"));
		criteria.addOrder(Order.asc("deviceNo"));
		return findByCriteria(page.getNo(), page.getSize(), criteria);
	}

	public String getDeviceNo() {
		String stime = DateUtil.nowtime2Str();
		return stime.replaceAll("-", "").replaceAll(" ", "")
				.replaceAll(":", "");
	}

	public void deleteById(long id) {
		// 不用删除，将设备信息显示成禁用状态
		Devices dbdev = getDevicesById(id);
		dbdev.setEnable(false);
		dao.update(dbdev);
		// dao.delete(getDevicesById(id));
	}

	public void toEnableDevice(long id) {
		Devices dbdev = getDevicesById(id);
		dbdev.setEnable(true);
		dao.update(dbdev);
	}

	public Devices getDevicesById(long id) {
		String hql = "from com.monlyu.vo.Devices d where d.id =?";
		return (Devices) dao.findOne(hql, id);
	}

	public Pages<DeviceSupply> getDeviceSupplys(Page page) {
		Criteria criteria = dao.initCriteria(DeviceSupply.class);
		criteria.add(Restrictions.eq("enable", true));
		criteria.addOrder(Order.asc("unitName"));
		return findByCriteria(page.getNo(), page.getSize(), criteria);

	}

	/**
	 * 查询所有有效的供方单位列表
	 * 
	 * @return
	 * @since chendan 2011-8-18
	 */
	public HashMap getDeviceSupplys() {
		String hql = "from com.monlyu.vo.DeviceSupply d where d.enable=1 order by d.id";
		List<DeviceSupply> results = dao.find(hql);
		HashMap dsHashMap = new HashMap();
		for (DeviceSupply deviceSupply : results) {
			dsHashMap.put(deviceSupply.getId(), deviceSupply.getUnitName());
		}
		return dsHashMap;
	}
}
